# PyMite Configuration
PLATFORM := $(notdir $(CURDIR))
PM_LIB_ROOT = pmvm_$(PLATFORM)
PM_LIB_FN = lib$(PM_LIB_ROOT).a
PM_LIB_PATH = ../../vm/$(PM_LIB_FN)
PM_USR_SOURCES = main.py events.py portio.py keyboard.py #speaker.py
PMIMGCREATOR := ../../tools/pmImgCreator.py
PMGENPMFEATURES := ../../tools/pmGenPmFeatures.py
IPM = true
DEBUG = true

TARGET = main
SOURCES = $(TARGET).c plat.c $(TARGET)_nat.c $(TARGET)_img.c
SOURCES_LIB  = lib/fixedQueue.c lib/idt.c lib/irq.c lib/isr.c sys/io.c
SOURCES_LIB += lib/math.c
SOURCES_LIB += GaiaLib/stdio.c GaiaLib/stdlib.c GaiaLib/string.c
SOURCES_DRV  = drivers/keyboard.c keyboard_layouts/us.c
SOURCES_DRV += drivers/PIT.c drivers/VGA.c
OBJS  = $(SOURCES:.c=.o) $(SOURCES_LIB:.c=.o) $(SOURCES_DRV:.c=.o)
OBJS += asm/boot.o asm/idt.o asm/irq.o asm/isr.o asm/interrupt.o

CWARN = -Wall -Wstrict-prototypes
#CWARN = -Wall -Wstrict-prototypes -Werror
CINCS  = -I$(abspath .) -I$(abspath .)/include -I$(abspath .)/GaiaLib/include
CINCS += -I$(abspath .)/drivers -I../../vm

ifeq ($(DEBUG),true)
	CDEBUGS += -g -D__DEBUG__=1
else
    CDEBUGS += -Os
endif

CFLAGS = $(CWARN) -fno-strict-aliasing \
         -Wdeclaration-after-statement $(CDEBUGS) $(CINCS) \
         -nostdinc -fno-builtin -nostdlib -fno-stack-protector -m32 -O2

ASFLAGS=-felf

export CFLAGS IPM PM_LIB_FN


.PHONY: all clean

all : pmfeatures.h $(TARGET).out

$(PM_LIB_PATH) : ../../vm/*.c ../../vm/*.h
	make -C ../../vm

$(TARGET).out : $(OBJS) $(PM_LIB_PATH)
	$(CC) -o $@ $(OBJS) $(PM_LIB_PATH) -m32 -nostdinc -nostdlib -fno-builtin
#	$(LD) -o $@ $(OBJS) $(PM_LIB_PATH) -Ttext 0x100000 -melf_i386

asm/boot.o:
	$(CC) -o $@ -c asm/boot.S $(CFLAGS)

asm/idt.o:
	nasm $(ASFLAGS) asm/idt.s

asm/irq.o:
	nasm $(ASFLAGS) asm/irq.s

asm/isr.o:
	nasm $(ASFLAGS) asm/isr.s

asm/interrupt.o:
	nasm $(ASFLAGS) asm/interrupt.s

pmfeatures.h : pmfeatures.py $(PMGENPMFEATURES)
	$(PMGENPMFEATURES) pmfeatures.py > $@

# Generate native code and module images from the python source
$(TARGET)_nat.c $(TARGET)_img.c: $(PM_USR_SOURCES) pmfeatures.py
	$(PMIMGCREATOR) -f pmfeatures.py -c -u -o $(TARGET)_img.c --native-file=$(TARGET)_nat.c $(PM_USR_SOURCES)

clean :
	$(MAKE) -C ../../vm clean
	$(RM) $(TARGET).out $(OBJS) $(TARGET)_img.* $(TARGET)_nat.* pmfeatures.h
